using System;
using System.Collections.Generic;
using BanZuiCloudControlAgent.Utils.Common;

namespace BanZuiCloudControlAgent.Utils.Common.Example
{
    /// <summary>
    /// JsonHelper使用示例类
    /// </summary>
    public class JsonHelperExample
    {
        /// <summary>
        /// 演示如何使用JsonHelper进行序列化和反序列化
        /// </summary>
        public static void DemoJsonHelper()
        {
            Console.WriteLine("=== JsonHelper示例 ===");

            // 创建测试对象
            var person = new Person
            {
                Id = 1,
                Name = "张三",
                Age = 30,
                Email = "zhangsan@example.com",
                IsActive = true,
                Addresses = new List<Address>
                {
                    new Address { City = "北京", Street = "朝阳区" },
                    new Address { City = "上海", Street = "浦东新区" }
                }
            };

            // 序列化对象为JSON字符串
            string json = JsonHelper.Serialize(person);
            Console.WriteLine("序列化结果:");
            Console.WriteLine(json);

            // 使用缩进格式序列化
            string prettyJson = JsonHelper.Serialize(person, true);
            Console.WriteLine("\n缩进格式序列化结果:");
            Console.WriteLine(prettyJson);

            // 反序列化JSON为对象
            var deserializedPerson = JsonHelper.Deserialize<Person>(json);
            Console.WriteLine("\n反序列化结果:");
            Console.WriteLine($"姓名: {deserializedPerson.Name}");
            Console.WriteLine($"年龄: {deserializedPerson.Age}");
            Console.WriteLine($"地址数量: {deserializedPerson.Addresses.Count}");

            // 尝试反序列化可能失败的JSON
            string invalidJson = "{\"name\": \"李四\", \"age\": \"invalid\"}";
            var defaultPerson = new Person { Name = "默认人员", Age = 0 };
            var result = JsonHelper.TryDeserialize(invalidJson, defaultPerson);
            Console.WriteLine("\n尝试反序列化无效JSON:");
            Console.WriteLine($"结果: {result.Name}, {result.Age}");

            // 对象类型转换
            var simplePerson = new SimplePerson
            {
                FullName = "王五",
                YearsOld = 25
            };

            var convertedPerson = JsonHelper.Convert<SimplePerson, Person>(simplePerson);
            Console.WriteLine("\n对象转换结果:");
            Console.WriteLine($"转换后姓名: {convertedPerson.Name}");
            Console.WriteLine($"转换后年龄: {convertedPerson.Age}");

            // 检查JSON有效性
            Console.WriteLine("\nJSON有效性检查:");
            Console.WriteLine($"有效JSON: {JsonHelper.IsValidJson(json)}");
            Console.WriteLine($"无效JSON: {JsonHelper.IsValidJson("{name: error}")}");
        }

        /// <summary>
        /// 演示自定义异常处理
        /// </summary>
        public static void DemoExceptionHandling()
        {
            Console.WriteLine("=== JsonHelper异常处理示例 ===");

            try
            {
                // 尝试反序列化无效JSON
                string invalidJson = "{\"name\": \"李四\", \"age\": \"invalid\"}";
                var person = JsonHelper.Deserialize<Person>(invalidJson);
                Console.WriteLine("这行不会执行");
            }
            catch (JsonSerializationException ex)
            {
                Console.WriteLine($"捕获到自定义异常: {ex.GetType().Name}");
                Console.WriteLine($"异常消息: {ex.Message}");
                Console.WriteLine($"数据类型: {ex.DataType}");
                Console.WriteLine($"操作类型: {ex.OperationType}");
                Console.WriteLine($"内部异常: {ex.InnerException?.GetType().Name}");
            }

            try
            {
                // 尝试序列化不可序列化的对象
                var circularObject = new CircularReference();
                circularObject.Reference = circularObject; // 创建循环引用
                string json = JsonHelper.Serialize(circularObject);
                Console.WriteLine("这行不会执行");
            }
            catch (JsonSerializationException ex)
            {
                Console.WriteLine($"\n捕获到自定义异常: {ex.GetType().Name}");
                Console.WriteLine($"异常消息: {ex.Message}");
                Console.WriteLine($"数据类型: {ex.DataType}");
                Console.WriteLine($"操作类型: {ex.OperationType}");
                Console.WriteLine($"内部异常: {ex.InnerException?.GetType().Name}");
            }
        }

        #region 测试类

        /// <summary>
        /// 测试用的人员类
        /// </summary>
        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public string Email { get; set; }
            public bool IsActive { get; set; }
            public List<Address> Addresses { get; set; } = new List<Address>();
        }

        /// <summary>
        /// 测试用的地址类
        /// </summary>
        public class Address
        {
            public string City { get; set; }
            public string Street { get; set; }
        }

        /// <summary>
        /// 简化的人员类，用于测试对象转换
        /// </summary>
        public class SimplePerson
        {
            public string FullName { get; set; }
            public int YearsOld { get; set; }
        }

        /// <summary>
        /// 包含循环引用的类，用于测试序列化异常
        /// </summary>
        public class CircularReference
        {
            public string Name { get; set; } = "循环引用";
            public CircularReference Reference { get; set; }
        }

        #endregion
    }
} 